package com.szrt.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginFilter implements Filter{
	
	//private FilterConfig config = null;

	@Override
	public void init(FilterConfig config) throws ServletException {
		//this.config = config;
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
			throws IOException, ServletException {
		
		HttpServletRequest request = (HttpServletRequest)req;
		HttpServletResponse response = (HttpServletResponse)res;
		HttpSession session = request.getSession(false);
		
		String method = request.getParameter("method");
		String uri = request.getRequestURI();
		if(null==session){
			if(uri.contains("login.jsp")||"login".equals(method)){
				chain.doFilter(request, response);
			}else{
				response.sendRedirect("login.jsp");	
			}
		}else{
			if("cancel".equals(method)){
				chain.doFilter(request, response);
			}else{
				request.getRequestDispatcher("success.jsp").forward(request, response);
			}
		}
		
	}
		
		
		

	@Override
	public void destroy() {
		
	}

}
